package com.heima.kafka.simple;

import org.apache.kafka.clients.consumer.ConsumerConfig;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.apache.kafka.clients.consumer.KafkaConsumer;
import org.apache.kafka.clients.producer.ProducerConfig;

import java.time.Duration;
import java.util.Collections;
import java.util.Properties;

public class ConsumerStart {

    public static void main(String[] args) {

        //2.创建连接信息
        Properties properties=new Properties();
        //kafka的连接地址
        properties.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG,"192.168.200.140:9092");
        //加入组id,必须得有，否则会报错
        properties.put(ConsumerConfig.GROUP_ID_CONFIG,"group2");
        //key序列化
        properties.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, "org.apache.kafka.common.serialization.StringDeserializer");
        properties.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, "org.apache.kafka.common.serialization.StringDeserializer");
        /*//手动提交偏移量
        properties.put(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG,"false");*/

        //1.创建消费者对象
        KafkaConsumer consumer = new KafkaConsumer(properties);

        //3.订阅主题
        consumer.subscribe(Collections.singleton("kafka-stream-consumer-topic"));

        while (true){
            //4.拉取消息
            ConsumerRecords<String,String> records = consumer.poll(Duration.ofMillis(10));//指定是10ms拉取一次
            for (ConsumerRecord<String, String> record : records) {
                System.out.println(record.key()+":"+record.value());
            }
        }

    }
}
